package org.ccu.eshop.controller.api;


import com.alibaba.fastjson.JSON;
import org.ccu.eshop.domain.CartSelectedMerPo;
import org.ccu.eshop.domain.dto.CartSelectedMerVo;
import org.ccu.eshop.domain.dto.CartVo;
import org.ccu.eshop.domain.dto.MemberVo;
import org.ccu.eshop.domain.dto.OrdersVo;
import org.ccu.eshop.service.ShopCarServiceltface;
import org.ccu.eshop.service.impl.ShopCarServiceimpl;
import org.ccu.eshop.utils.AjaxResultUtil;
import org.ccu.eshop.utils.SessionUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspApplicationContext;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/auth/shopCar")
public class ShopCarController  extends HttpServlet {

    ShopCarServiceltface shopCarService=new ShopCarServiceimpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("m");
        if("list".equals(method)){
            list(request,response);
        }else if("add".equals(method)){
            add(request,response);
        }else if("delete".equals(method)){
            delete(request,response);
        }else if("clear".equals(method)){
            clear(request,response);
        }else if("updateNum".equals(method)){
            updateNum(request,response);
        }else if("submit".equals(method)){
            submitShopCar(request,response);
        }
    }
    /**
     * 购物车订单提交
     * @param request
     * @param response
     */
    private void submitShopCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartVo cartVo=SessionUtil.getShopCarInfo(request);
        MemberVo memberVo=SessionUtil.getLoginMemberInfo(request);
        OrdersVo ordersVo=shopCarService.submitShopCar(cartVo.getId(),memberVo.getId());
        if(ordersVo!=null){
            AjaxResultUtil result=AjaxResultUtil.success("购物车订单提交成功");
            //需要重新绑定购物车
            //创建购物车  获得
            CartVo cartVo2=shopCarService.getShopCar(memberVo.getId());
            HttpSession session=  request.getSession(true);
            session.setAttribute("shopCart",cartVo2);
            result.put("data",ordersVo);
            response.getWriter().write(JSON.toJSONString(result));
        }else{
            response.getWriter().write(JSON.toJSONString(AjaxResultUtil.error("购物车订单提交失败---")));
        }
    }


    /**
     * 修改商品数量
     * @param request
     * @param response
     */
    private void updateNum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           String id= request.getParameter("id");
           String num=request.getParameter("num");
           CartVo cartVo=SessionUtil.getShopCarInfo(request);
           boolean b=shopCarService.updateCartSelectedMerNumById(id,Integer.parseInt(num),cartVo.getId());
        if(b){
            AjaxResultUtil result=AjaxResultUtil.success("购物车内的商品数量修改成功");
            response.getWriter().write(JSON.toJSONString(result));
        }else{
            response.getWriter().write(JSON.toJSONString(AjaxResultUtil.error("删除购物车数量修改失败---")));
        }
    }

    /**
     * 清空购物车
     * @param request
     * @param response
     */
    private void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        CartVo cartVo=SessionUtil.getShopCarInfo(request);
        boolean b=shopCarService.clearCartSelectedMerById(cartVo.getId());
        if(b){
            AjaxResultUtil result=AjaxResultUtil.success("购物车内的商品清空成功");
            response.getWriter().write(JSON.toJSONString(result));
        }else{
            response.getWriter().write(JSON.toJSONString(AjaxResultUtil.error("删除购物车清空失败---")));
        }
    }

    /**
     * 删除购物车内的商品项
     * @param request
     * @param response
     */
    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
        CartVo cartVo=SessionUtil.getShopCarInfo(request);
        boolean b=shopCarService.deleteCartSelectedMerById(id,cartVo.getId());
        if(b){
            AjaxResultUtil result=AjaxResultUtil.success("购物车内的商品删除成功-id="+id);
            response.getWriter().write(JSON.toJSONString(result));
        }else{
            response.getWriter().write(JSON.toJSONString(AjaxResultUtil.error("删除购物车失败---")));
        }


    }

    /**
     * 往购物车内添加商品
     * @param request
     * @param response
     */
    private void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        String merId=request.getParameter("merId");
        CartVo cartVo=SessionUtil.getShopCarInfo(request);
        MemberVo memberVo=SessionUtil.getLoginMemberInfo(request);
        boolean b=shopCarService.addShopCar(cartVo.getId(),merId,memberVo.getFavourable());
        if (b){
            AjaxResultUtil result=AjaxResultUtil.success("商品添加入购物车成功-id="+merId);
            response.getWriter().write(JSON.toJSONString(result));
        }else{
            response.getWriter().write(JSON.toJSONString(AjaxResultUtil.error("添加购物车失败---")));
        }


    }


    /**
     * 购物车列表
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    private void list(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{
        AjaxResultUtil result=AjaxResultUtil.success("购物车列表获取成功-");
        //获得登录信息
        MemberVo memberVo= SessionUtil.getLoginMemberInfo(request);
        //获得购物车信息
        CartVo cartVo=SessionUtil.getShopCarInfo(request);
       List<CartSelectedMerVo> cartSelectedMerList= shopCarService.getCarSelectMerListByCar(cartVo.getId());//根据购物车的id来查询购物车信息
        result.put("data",cartSelectedMerList);
        response.getWriter().write(JSON.toJSONString(result));

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
